﻿using SD.Infrastructure.EntityBase;
using SlamDunk.ValueObjects.Enums;
using System;

namespace SlamDunk.MES.Domain.Entities.ConfigurationContext
{
    /// <summary>
    /// 硬件连接
    /// </summary>
    public class HardwareConnection : PlainEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected HardwareConnection() { }
        #endregion

        #region 01.创建硬件网口连接构造器
        /// <summary>
        /// 创建硬件网口连接构造器
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="port">端口号</param>
        public HardwareConnection(Guid? terminalId, string ipAddress, int? port)
            : this()
        {
            this.TerminalId = terminalId;
            this.IPAddress = ipAddress;
            this.Port = port;
            this.ConnectionMode = HardwareConnectionMode.Network;

            //清空串口
            this.ComPort = null;
            this.BaudRate = null;
            this.StopBit = null;
            this.ParityBit = null;
            this.DataBit = null;
            this.DTR = null;
            this.RTS = null;
        }
        #endregion

        #region 02.创建硬件串口连接构造器
        /// <summary>
        /// 创建硬件串口连接构造器
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="comPort">串口端口号</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="stopBit">停止位</param>
        /// <param name="parityBit">校验位</param>
        /// <param name="dataBit">数据位</param>
        /// <param name="dtr">数据终端准备</param>
        /// <param name="rts">请求发送</param>
        public HardwareConnection(Guid? terminalId, string comPort, int? baudRate, StopBits? stopBit, Parity? parityBit, int? dataBit, bool? dtr, bool? rts)
            : this()
        {
            this.TerminalId = terminalId;
            this.ComPort = comPort;
            this.BaudRate = baudRate;
            this.StopBit = stopBit;
            this.ParityBit = parityBit;
            this.DataBit = dataBit;
            this.DTR = dtr;
            this.RTS = rts;
            this.ConnectionMode = HardwareConnectionMode.SerialPort;

            //清空网口
            this.IPAddress = null;
            this.Port = null;
        }
        #endregion

        #region 03.创建硬件USB口连接构造器
        /// <summary>
        /// 创建硬件USB口连接构造器
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        public HardwareConnection(Guid? terminalId)
            : this()
        {
            this.TerminalId = terminalId;
            this.ConnectionMode = HardwareConnectionMode.USB;

            //清空网口
            this.IPAddress = null;
            this.Port = null;
        }
        #endregion

        #endregion

        #region # 属性

        #region 终端Id —— Guid? TerminalId
        /// <summary>
        /// 终端Id
        /// </summary>
        public Guid? TerminalId { get; private set; }
        #endregion

        #region 连接模式 —— HardwareConnectionMode ConnectionMode
        /// <summary>
        /// 连接模式
        /// </summary>
        public HardwareConnectionMode ConnectionMode { get; private set; }
        #endregion

        #region IP地址 —— string IPAddress
        /// <summary>
        /// IP地址
        /// </summary>
        public string IPAddress { get; private set; }
        #endregion

        #region 端口号 —— int? Port
        /// <summary>
        /// 端口号
        /// </summary>
        public int? Port { get; private set; }
        #endregion

        #region 串口端口号 —— string ComPort
        /// <summary>
        /// 串口端口号
        /// </summary>
        public string ComPort { get; private set; }
        #endregion

        #region 波特率 —— int? BaudRate
        /// <summary>
        /// 波特率
        /// </summary>	
        public int? BaudRate { get; private set; }
        #endregion	

        #region 停止位 —— StopBits? StopBit
        /// <summary>
        /// 停止位
        /// </summary>	
        public StopBits? StopBit { get; private set; }
        #endregion	

        #region 校验位 —— Parity? ParityBit
        /// <summary>
        /// 校验位
        /// </summary>	
        public Parity? ParityBit { get; private set; }
        #endregion	

        #region 数据位 —— int? DataBit
        /// <summary>
        /// 数据位
        /// </summary>	
        public int? DataBit { get; private set; }
        #endregion	

        #region 数据终端准备 —— bool? DTR
        /// <summary>
        /// 数据终端准备
        /// </summary>	
        public bool? DTR { get; private set; }
        #endregion	

        #region 请求发送 —— bool? RTS
        /// <summary>
        /// 请求发送
        /// </summary>	
        public bool? RTS { get; private set; }
        #endregion

        #region 外键属性 - 硬件Id —— Guid HardwareId
        /// <summary>
        /// 外键属性 - 硬件Id
        /// </summary>
        public Guid HardwareId { get; private set; }
        #endregion

        #region 导航属性 - 硬件 —— Hardware Hardware
        /// <summary>
        /// 导航属性 - 硬件
        /// </summary>
        public virtual Hardware Hardware { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 配置网口通信 —— void ConfigureNetworkMode(Guid? terminalId, string ipAddress...
        /// <summary>
        /// 配置网口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="port">端口号</param>
        public void ConfigureNetworkMode(Guid? terminalId, string ipAddress, int port)
        {
            this.ConnectionMode = HardwareConnectionMode.Network;
            this.TerminalId = terminalId;
            this.IPAddress = ipAddress;
            this.Port = port;

            //清空串口
            this.ComPort = null;
            this.BaudRate = null;
            this.StopBit = null;
            this.ParityBit = null;
            this.DataBit = null;
            this.DTR = null;
            this.RTS = null;
        }
        #endregion

        #region 配置串口通信 —— void ConfigureSerialPortMode(Guid? terminalId, string comPort...
        /// <summary>
        /// 配置串口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="comPort">串口端口号</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="stopBit">停止位</param>
        /// <param name="parityBit">校验位</param>
        /// <param name="dataBit">数据位</param>
        /// <param name="dtr">数据终端准备</param>
        /// <param name="rts">请求发送</param>
        public void ConfigureSerialPortMode(Guid? terminalId, string comPort, int baudRate, StopBits stopBit, Parity parityBit, int dataBit, bool dtr, bool rts)
        {
            this.ConnectionMode = HardwareConnectionMode.SerialPort;
            this.TerminalId = terminalId;
            this.ComPort = comPort;
            this.BaudRate = baudRate;
            this.StopBit = stopBit;
            this.ParityBit = parityBit;
            this.DataBit = dataBit;
            this.DTR = dtr;
            this.RTS = rts;

            //清空网口
            this.IPAddress = null;
            this.Port = null;
        }
        #endregion

        #region 配置USB口通信 —— void ConfigureUsbMode(Guid? terminalId)
        /// <summary>
        /// 配置USB口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        public void ConfigureUsbMode(Guid? terminalId)
        {
            this.ConnectionMode = HardwareConnectionMode.SerialPort;
            this.TerminalId = terminalId;

            //清空网口
            this.IPAddress = null;
            this.Port = null;

            //清空串口
            this.ComPort = null;
            this.BaudRate = null;
            this.StopBit = null;
            this.ParityBit = null;
            this.DataBit = null;
            this.DTR = null;
            this.RTS = null;
        }
        #endregion

        #endregion
    }
}
